<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>购物车</title>
    <meta name="viewport" content="initial-scale=1, maximum-scale=1">
    <link rel="shortcut icon" href="/favicon.ico">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">

    <link rel="stylesheet" href="//g.alicdn.com/msui/sm/0.6.2/css/sm.min.css">
    <link rel="stylesheet" href="//g.alicdn.com/msui/sm/0.6.2/css/sm-extend.min.css">

    <style>
        *{
            padding:0;
            margin:0;
        }
        html,body{
            width:100%;
            height:100%;
        }


        .fl{
            float:left;
        }
        .clearfix:after{
            display:table;
            content:"";
            clear:both;
        }
        .product{
            display:flex;
            justify-content:space-between;
            align-items:center;
            margin:0.2rem;
            background: #fff;
            border-radius: 0.3rem;
            padding:0.2rem;
            opacity:1;
            transition: all 0.2s;

        }
        .selectImg{
            margin:0.3rem;
        }
        .productImage{
            margin:0.3rem;
        }
        .productInfoDiv{
            padding:0 0.2rem;
        }
        .titleDiv{
            font-size:14px;
            height:2.2rem;
            line-height:1.1rem;
            overflow: hidden;
        }
        .priceAndCountDiv{
            display:flex;
            justify-content: space-between;
            margin-right:0.7rem;
            line-height:1.5rem;
            height:1.5rem;

        }
        .deleteDiv{
            float:right;
            margin-right:0.6rem ;

        }
        .content{
            position: fixed;
            bottom:5rem;
            overflow-y:auto;
        }
        .allselect{
            position:fixed;
            height:2.2rem;
            bottom:2.5rem;
            background:#fff;
            width:100%;
            display:flex;
            justify-content: space-between;
            padding: 0 0.3rem;
            align-content: center;
            line-height:2.2rem;

        }
        #allSelectImg{
            margin-right:0.3rem;
        }
        .nav{
            position:fixed;
            bottom:0;
            background:#fff;
            height:2.5rem;
            border-top:0.05rem solid #efeff4;
            width:100%;

        }
        .nav ul li{
            float:left;
            width:25%;
            text-align: center;
            list-style: none;
            font-size: 12px;
        }
        .nav ul li img{
            width:1rem;
            margin-top: 0.3rem;
        }
        .nav ul li p{
            margin:0;
            line-height:0.7rem;
        }
        .sum{
            width:4.5rem;
            height:1.8rem;
            background:linear-gradient(#f68c12,#f55601);
            border:none;
            border-radius: 0.9rem;
            color:#fff;
            margin-right: 0.2rem;
        }
        #emptyCar{
            display:flex;
            justify-content: space-between;

        }

    </style>

</head>
<body>
<div class="page-group">
    <div class="page page-current">
        <header class="bar bar-nav">
            <h1 class="title" id="shopCarCount">购物车</h1>
        </header>
        <!-- 你的html代码 -->
        <!--class="content" 可以出现滚动条-->
        <div class="content">
            <div id="shopCarDiv"></div>

            <div id="emptyCar" style="display:none">
                您的购物车饿扁啦！快去<a href="index.html">逛逛</a>吧
            </div>
        </div>

        <div class="allselect"><div><img style="vertical-align: middle;width:1rem;" id="allSelectImg" src="" />
            <span>全选</span></div>
            <div><span>合计:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
             <input class="sum" type="button" value="结算" onclick="generateOrder()">
            </div>
        </div>
        <div class="nav">
            <ul>
                <li><img src="images/11.png" id="home"/><p>首页</p></li>
                <li><img src="images/22.png" id="class1"/><p>分类</p></li>
                <li><img src="images/33.png" id="car"/><p>购物车</p></li>
                <li><img src="images/44.png" id="my"/><p>我的</p></li>
            </ul>
        </div>

    </div>
</div>

<script type='text/javascript' src='//g.alicdn.com/sj/lib/zepto/zepto.min.js' charset='utf-8'></script>
<script type='text/javascript' src='//g.alicdn.com/msui/sm/0.6.2/js/sm.min.js' charset='utf-8'></script>
<script type='text/javascript' src='//g.alicdn.com/msui/sm/0.6.2/js/sm-extend.min.js' charset='utf-8'></script>
<script>
    home.onclick = function(){
        if(this.src=="images/111.png") {
            this.src = "images/11.png";
        }else{
            this.src="images/111.png";
            location="123.html";
        }
    };
    class1.onclick = function(){
        if(this.src=="images/222.png") {
            this.src = "images/22.png";
        }else{
            this.src="images/222.png";
            location="select.html";
        }
    };
    car.onclick = function(){
        if(this.src=="images/333.png") {
            this.src = "images/33.png";
        }else{
            this.src="images/333.png";
            location="cart.html";
        }
    };
    my.onclick = function(){
        if(this.src=="images/444.png") {
            this.src = "images/44.png";
        }else{
            this.src="images/444.png";
            location="my.html";
        }
    }
</script>
<script>
    function generateOrder(){
        var p = shopCar[0];
        location='http://test.hfjava.com/shop/zfbpay?' +
            'price='+p.price+'&' +
            'subject='+"Under Armour"+'&' +
            'body=Under Armour';
    }

    var noneSelectImgSrc="";
    var selectImgSrc="";
    var shopCar;

    allSelectImg.onclick = function(){
        this.src = this.src ==selectImgSrc?
            noneSelectImgSrc:selectImgSrc;

        var allSelectImgSrc = this.src;
        $(".ckimg").each(function(index,ele){
            ele.src = allSelectImgSrc;
        })
    };

    window.onload = function(){
        $.showPreloader();
        $("#shopCarDiv").hide();
        var loadedImageCount = 0;

        shopCar = JSON.parse(localStorage.getItem("shopCar"));
        if(!shopCar|| shopCar.length==0) {
            emptyCar.style.display = "block";
            $.hidePreloader();
        }else{
            shopCarCount.innerHTML ="购物车("+shopCar.length+")";
            for(var i = 0; i<shopCar.length; i++){
                var product = shopCar[i];
                var productDiv = document.createElement("div");
                productDiv.className="product";

                var selectImg = document.createElement("img");
                selectImg.src=noneSelectImgSrc;
                selectImg.style.height = "1rem";
                selectImg.className="ckimg";
                selectImg.onclick = function(){
                 //   this.src=this.src == selectImgSrc?noneSelectImgSrc:selectImgSrc;

                    if(this.src==selectImgSrc) {
                        this.src = noneSelectImgSrc;
                    }else{
                        this.src=selectImgSrc;
                    }
                };

                var productImage = document.createElement("img");
                productImage.className="productImage";
                productImage.src = product.pic;
                productImage.style.height = "5rem";
                productImage.onload =function(){
                    loadedImageCount++;
                    if(loadedImageCount==shopCar.length){
                        $.hidePreloader();
                        $("#shopCarDiv").show(300);
                    }
                };

                var productInfoDiv = document.createElement("div");
                productInfoDiv.className="productInfoDiv";

                var titleDiv = document.createElement("div");
                titleDiv.className = "titleDiv";
                titleDiv.innerHTML = product.title;

                var priceAndCountDiv = document.createElement("div");
                priceAndCountDiv.className = "priceAndCountDiv";



                var priceSpan = document.createElement("span");
                priceSpan.innerHTML = "&yen; " + product.price;
                priceSpan.style.color = "#ff5700";

               // var countSpan = document.createElement("span");
                //countSpan.innerHTML = "x " + product.count;

                var numDiv = document.createElement("div");
                var jianDiv = document.createElement("input");
                jianDiv.className = "jianDiv";
                jianDiv.type = "button";
                jianDiv.value = "-";
                jianDiv.style.width = "1rem";
                jianDiv.style.background = "#fff";
                jianDiv.style.border = "1px solid #ccc";
                var numTxt = document.createElement("input");
                numTxt.className = "numTxt";
                numTxt.type="text";
                numTxt.value = product.count;
                numTxt.style.padding = "0 0.2rem";
                numTxt.style.width = "3em";
                numTxt.style.background = "#fff";
                numTxt.style.border = "1px solid #ccc";
                var jiaDiv =document.createElement("input");
                jiaDiv.className = "jiaDiv";
                jiaDiv.type = "button";
                jiaDiv.value="+";
                jiaDiv.style.width = "1rem";
                jiaDiv.style.background = "#fff";
                jiaDiv.style.border = "1px solid #ccc";

                var deleteImg = document.createElement("input");
                deleteImg.type = "image";
                deleteImg.className = "deleteDiv";
                deleteImg.src = "images/delete.png";
                deleteImg.nid=product.nid;
                deleteImg.onclick = function(){
                    deleteProductFormShopCart(this,this.nid);
                };

                numDiv.appendChild(jianDiv);
                numDiv.appendChild(numTxt);
                numDiv.appendChild(jiaDiv);
                numDiv.appendChild(deleteImg);
                priceAndCountDiv.appendChild(priceSpan);
              //  priceAndCountDiv.appendChild(countSpan);
                productInfoDiv.appendChild(titleDiv);
                productInfoDiv.appendChild(priceAndCountDiv);
                productInfoDiv.appendChild(numDiv);
                productDiv.appendChild(selectImg);
                productDiv.appendChild(productImage);
                productDiv.appendChild(productInfoDiv);
                shopCarDiv.appendChild(productDiv);
            }
        }
        var jians = document.getElementsByClassName("jianDiv");
        var txts1 = document.getElementsByClassName("numTxt");
        var jias = document.getElementsByClassName("jiaDiv");

        for(var j=0;j<jians.length;j++){
            jians[j].index = j;
            jians[j].onclick = function(){
                var t = this.index;
                if(parseInt(txts1[t].value)==1){
                    txts1[t].value=1;
                    return;
                }
                txts1[t].value = parseInt(txts1[t].value)-1;
            }
        }
        for(var j=0;j<jias.length;j++){
            jias[j].index = j;
            jias[j].onclick = function(){
                var t = this.index;
                txts1[t].value = parseInt(txts1[t].value)+1;
            }
        }
    };


    function deleteProductFormShopCart(deleteImg,nid){

        for(var i=0;i<shopCar.length;i++) {

            if (shopCar[i].nid == nid) {
                $.confirm('您确定要移除该宝贝吗？', function () {
                    shopCar.splice(i, 1);

                    $(deleteImg).parents(".product").hide();
                    shopCarCount.innerHTML = "购物车("+shopCar.length+")";
                    $.toast("移除成功!");
                    if (shopCar.length == 0) {
                        emptyCar.style.display = "block";
                    }
                    localStorage.setItem("shopCar", JSON.stringify(shopCar));
                });
                break;
            }
        }
    }

</script>
</body>
</html>